package com.lieve.servicea.config;

import org.redisson.Redisson;
import org.redisson.api.RLock;

import java.util.concurrent.TimeUnit;

/**
 * @author sunlijiang
 * @date 2020/1/4
 */
public class DistributedRedisLock {

    private static Redisson redisson = RedissonManager.getRedisson();
    private static final String LOCK_PREFIX = "redisLock:";

    public static boolean acquire(String lockName) {
        String lockKey = LOCK_PREFIX + lockName;
        RLock lock = redisson.getLock(lockKey);
        lock.lock(10, TimeUnit.SECONDS);
        System.out.println("********lock******" + Thread.currentThread().getName());
        return true;
    }

    public static void release(String lockName) {
        String lockKey = LOCK_PREFIX + lockName;
        RLock lock = redisson.getLock(lockKey);
        lock.unlock();;
        System.out.println("********unlock******" + Thread.currentThread().getName());
    }
}
